<div id="<?= $this->getId() ?>" style="width: <?= $mapWidth ?>;height: <?= $mapHeight ?>"></div>
<script type="text/javascript">
    window._AMapSecurityConfig = {
        securityJsCode:'<?= $amapJscode ?>',
    }
</script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=<?= $amapKay ?>&plugin=AMap.Geocoder"></script>
<script type="text/javascript">
    var lng=$("[data-field-name=<?= $longitudeCode ?>] input").val();
    var lat=$("[data-field-name=<?= $latitudeCode ?>] input").val();
    if(!lng){
        lng=116.397428;
    }
    if(!lat){
        lat=39.90923;
    }

    var map = new AMap.Map("<?= $this->getId() ?>", {
        resizeEnable: true, //是否监控地图容器尺寸变化
        zoom:<?= $mapZoom ?>, //初始化地图层级
        center: [lng, lat] //初始化地图中心点
    });

    var geocoder = new AMap.Geocoder({
        //city: "010", //城市设为北京，默认：“全国”
        radius: 1000 //范围，默认：500
    });
    var marker = new AMap.Marker();
    // 实例化点标记
    addMarker();
    function addMarker() {
        var lnglat  = [lng, lat];
        map.add(marker);
        marker.setPosition(lnglat);
    }
    function regeoCode() {

        var lnglat  = [lng, lat];
        map.add(marker);
        marker.setPosition(lnglat);
        geocoder.getAddress(lnglat, function(status, result) {

            if (status === 'complete'&&result.regeocode) {
                var address = result.regeocode.formattedAddress;
                $("[data-field-name=<?= $addressCode ?>] input").val(address);
            }else{
                console.log(result);
                console.log('根据经纬度查询地址失败');
            }
        });
    }

    map.on('click',function(e){
        lng=e.lnglat.getLng();
        lat=e.lnglat.getLat();
        $("[data-field-name=<?= $longitudeCode ?>] input").val(lng);
        $("[data-field-name=<?= $latitudeCode ?>] input").val(lat);
        regeoCode();
    })
</script>


